<?php

// / 如何获取文章正文
// / 通过设定的编码进行转码 如果转码成功则将 $charset_flag 设置为 1
// / 如果没有设定编码或者转码失败 则自动识别编码 识别成功 将$charset_flag 设置为 2 如果识别失败则将 $charset_flag 设置为 -2
function get_ip() {
	return null ;
}
function send_http($url, $timeout = 10, $connecttimeout = 5) {
	$ch = curl_init ();
	curl_setopt ( $ch, CURLOPT_URL, $url );
	curl_setopt ( $ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; rv:19.0) Gecko/20100101 Firefox/19.0' );
	curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, true );
	curl_setopt ( $ch, CURLOPT_FOLLOWLOCATION, true );
	curl_setopt ( $ch, CURLOPT_CONNECTTIMEOUT, $connecttimeout );
	curl_setopt ( $ch, CURLOPT_TIMEOUT, $timeout );
	$rs = curl_exec ( $ch );
	curl_close ( $ch );
	return $rs;
}
function get_content_file($site_url) {
	$ch = curl_init ( $site_url );
	curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 );
	$ip = get_ip ();
	if ($ip) {
	}
	curl_setopt ( $ch, CURLOPT_TIMEOUT, 30 );
	curl_setopt ( $ch, CURLOPT_CONNECTTIMEOUT, 5 );
	curl_setopt ( $ch, CURLOPT_ENCODING, "gzip" );
	curl_setopt ( $ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; rv:19.0) Gecko/20100101 Firefox/19.0' );
	curl_setopt ( $ch, CURLOPT_FOLLOWLOCATION, 1 ); // 获取转向后的内容
	$content = curl_exec ( $ch );
	
	if (FALSE === $content || empty ( $content )) {
		$http_info = curl_getinfo ( $ch );
		// / var_dump($http_info) ;
		$total_time = $http_info ['total_time'];
		$log_name = "request_fail";
		$msg = $site_url . "\n" . $total_time;
		// /admin_log ( $msg, $log_name );
		curl_close ( $ch );
		return FALSE;
	}
	
	$http_info = curl_getinfo ( $ch );
	curl_close ( $ch );
	$http_code = $http_info ['http_code'];
	if ($http_code != 200) {
		echo $http_code . PHP_EOL;
		return FALSE;
	}
	
	$content_type = $http_info ["content_type"];
	if (! (strpos ( $content_type, "text\/html" ) === FALSE)) {
		return FALSE;
	}
	
	$content = pre_handle ( $content );
	$charset = "";
	
	$charset_pattern = "#<meta.*?charset=[\"|']{0,1}(.*?)[\"|'|\s|;|>]# i";
	if (preg_match ( $charset_pattern, $content, $match )) {
		$charset = $match [1];
	}
	
	if (empty ( $charset )) {
		$charset_pattern = '/charset=(.*?)$/ i';
		if (preg_match ( $charset_pattern, $content_type, $match )) {
			$charset = $match [1];
			// admin_log ( $site_url, "charset_from_httpinfo" );
		}
	}
	
	if (! empty ( $charset )) {
		$charset = strtoupper ( $charset );
		if (! ("UTF-8" === $charset)) {
			$content = mb_convert_encoding ( $content, "UTF-8", $charset );
		}
	} else {
		// admin_log ( $site_url, "no_charset.txt" );
	}
	
	$redirect_url = $http_info ['url'];
	$data ["content"] = $content;
	$data ["flag"] = TRUE;
	
	$page_a_info = get_a_tag ( $redirect_url, $content );
	$data ['info_A'] = $page_a_info;
	
	if (strlen ( $content ) > 1000) {
		$tmp = substr ( $content, 0, 1000 );
	} else {
		$tmp = $content;
	}
	
	// admin_log ( $content, "testtesttest" );
	$data ['redirecturl'] = $redirect_url;
	
	return $data;
}
function pre_handle($content) {
	$content = stripslashes ( $content );
	$content = str_replace ( "\r\n", "#a#b#c#d", $content );
	$content = str_replace ( "\n\r", "#a#b#c#d", $content );
	$content = str_replace ( "\n", "#a#b#c#d", $content );
	$content = str_replace ( "\r", "#a#b#c#d", $content );
	$content = preg_replace ( "/<!--\[if.*?<!\[endif\]-->/is", "", $content );
	$content = preg_replace ( "/<!--.*?-->/", "", $content );
	$content = preg_replace ( "/<style(.*?)<\/style>/is", "", $content );
	$content = preg_replace ( "/<script(.*?)<\/script>/is", "", $content );
	return $content;
}
// 
//	检测网页是否有跳转  如 <meta http-equiv="refresh" content="5;url=hello.html"> 
//  




